home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / chat / reflect.000 / reflect / 3.0b3 / client.c next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  6.7 KB  |  327 lines

  1. /*
  2.  
  3. Copyright 1993, 1994, Cornell University
  4.  
  5. Cornell hereby grants permission to use, copy, modify, and distribute this program for any purpose 
  6. and without fee, provided that these copyright and permission notices appear on all copies and 
  7. supporting documentation, the name of Cornell not be used in advertising or publicity pertaining 
  8. to distribution of the program without specific prior permission, notice be given in supporting 
  9. documentation that copying and distribution is by permission of Cornell.  CORNELL MAKES NO 
  10. REPRESENTATIONS OR WARRANTEES, EXPRESS OR IMPLIED.  By way of example, but not limitation, 
  11. CORNELL MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR 
  12. PURPOSE OR THAT THE USE OF THIS SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, 
  13. TRADEMARKS, OR OTHER RIGHTS.  Cornell shall not be held liable for any liability with respect to 
  14. any claim by the user or any other party arising from use of the program.
  15.  
  16. This material is partially based on work sponsored by the National Science Foundation under Cooperative 
  17. Agreement No. NCR-9318337.  The government has certain rights in this material.
  18.  
  19. */
  20.  
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <varargs.h>
  25. #include <signal.h>
  26. #include <errno.h>
  27. #include <malloc.h>
  28. #include <sys/types.h>
  29. #include <sys/param.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/socket.h>
  32. #include <sys/time.h>
  33.  
  34. #ifndef LINUX
  35. #include <sys/socketvar.h>
  36. #include <net/route.h>
  37. #endif
  38.  
  39. #include <net/if.h>
  40. #include <netinet/in.h>
  41.  
  42. #include "reflect.h"
  43. #include "refmon.h"
  44. #include "globals.h"
  45.  
  46. short get_client_id()
  47. {
  48.     client *ctmp;
  49.     short  client_id,id_not_set;
  50.  
  51.     client_id = 1;
  52.     id_not_set = 1;
  53.  
  54.     /* find the next free client id  */
  55.  
  56.     while (id_not_set)
  57.     {
  58.        ctmp = chead;
  59.        while (ctmp != NULL)
  60.        {
  61.          if (ctmp->clnt_id == client_id)
  62.          {
  63.             client_id++;
  64.             break;
  65.          }
  66.          ctmp = ctmp->clnt_nptr;
  67.        }
  68.        if (ctmp == NULL)
  69.          id_not_set = 0;
  70.     }
  71.  
  72.     return(client_id);
  73. }
  74.  
  75.  
  76. client *find_client(caddr) 
  77.     unsigned long caddr;
  78. {
  79.     client *ctmp;
  80.  
  81.     ctmp = chead;
  82.  
  83.     while (ctmp != NULL)
  84.     {
  85.       if (ctmp->clnt_addr.addr == caddr)
  86.          return(ctmp);
  87.       ctmp = ctmp->clnt_nptr;
  88.     }
  89.     return(NULL);
  90. }
  91.  
  92.  
  93. void hold_down_client(cltptr)
  94.    client *cltptr;
  95. {
  96.     dolog("holding down client %s\n",cltptr->clnt_config.name);
  97.     cltptr->clnt_flags |= HOLD_DOWN;
  98.     cltptr->clnt_hdloop = 0;
  99. }
  100.  
  101.  
  102. void delete_client(cltptr)
  103.    client *cltptr;
  104. {
  105.     client *ctmp;
  106.     slist  *stmp,*stmp1;
  107.  
  108.     dolog("deleting client %s\n",cltptr->clnt_config.name);
  109.  
  110.     chead = unlink_client(chead,cltptr);
  111.  
  112.     if ((cltptr->clnt_flags & DELETE) == 0)
  113.     {
  114.        dolog("delete_client: client not found\n");
  115.        exit(-1);
  116.     }
  117.  
  118.     ctmp = chead;
  119.     while (ctmp != NULL)
  120.     {
  121.        ctmp->clnt_vlist = unlink_slist(ctmp->clnt_vlist,cltptr);
  122.        ctmp->clnt_alist = unlink_slist(ctmp->clnt_alist,cltptr);
  123.        ctmp = ctmp->clnt_nptr;
  124.     }
  125.  
  126.     stmp = cltptr->clnt_vlist;
  127.     while (stmp != NULL)
  128.     {
  129.        stmp1 = stmp;
  130.        stmp = stmp->snd_nptr;
  131.        free_slist(stmp1);
  132.     }
  133.  
  134.     stmp = cltptr->clnt_alist;
  135.     while (stmp != NULL)
  136.     {
  137.        stmp1 = stmp;
  138.        stmp = stmp->snd_nptr;
  139.        free_slist(stmp1);
  140.     }
  141.  
  142.     if (cltptr->clnt_flags & CLIENT)
  143.     {
  144.        client_cnt--;
  145.  
  146.        if (cltptr->clnt_config.sendMode)
  147.           send_ccnt--; 
  148.        else
  149.           lurker_ccnt--;
  150.     }
  151.     else
  152.     if (cltptr->clnt_flags & BCC_CLIENT)
  153.        bcc_client_cnt--;
  154.     else
  155.     if (cltptr->clnt_flags & BCC_SERVER)
  156.        bcc_client_cnt--;
  157.     else
  158.     if (cltptr->clnt_flags & BCC_ORIGIN)
  159.        bcc_client_cnt--;
  160.     else
  161.     if (cltptr->clnt_flags & REF1_CLIENT)
  162.        ref1_client_cnt--;
  163.     else
  164.     if (cltptr->clnt_flags & REF1_SERVER)
  165.        ref1_server_cnt--;
  166.     else
  167.     if (cltptr->clnt_flags & REF1_ORIGIN)
  168.        ref1_origin_cnt--;
  169.     else
  170.     if (cltptr->clnt_flags & REF2_SERVER)
  171.        ref2_server_cnt--;
  172.     else
  173.     if (cltptr->clnt_flags & REF2_ORIGIN)
  174.        ref2_origin_cnt--;
  175.     else
  176.     if (cltptr->clnt_flags & REF3_SERVER)
  177.        ref3_server_cnt--;
  178.     else
  179.     if (cltptr->clnt_flags & REF3_ORIGIN)
  180.        ref3_origin_cnt--;
  181.     else
  182.     if (cltptr->clnt_flags & (NV_UCLIENT | NV_MCLIENT))
  183.        nv_client_cnt--;
  184.     else
  185.     if (cltptr->clnt_flags & BCC_GCLIENT)
  186.        bcc_gclient_cnt--;
  187.     else
  188.     {
  189.        dolog("delete_client: unknown type\n");
  190.        exit(-1);
  191.     }
  192.  
  193.     free_client(cltptr);
  194. }
  195.  
  196.  
  197. slist *unlink_slist(shead,cltptr)
  198.     slist *shead;
  199.     client *cltptr;
  200. {
  201.     slist *stmp;
  202.  
  203.     if (shead == NULL)
  204.        return(NULL);
  205.     else
  206.        if (shead->snd_client == cltptr)
  207.        {
  208.           stmp = shead->snd_nptr;
  209.           free_slist(shead);
  210.           return(stmp);
  211.        }
  212.  
  213.     shead->snd_nptr = unlink_slist(shead->snd_nptr,cltptr);
  214.     return(shead);
  215. }
  216.  
  217.  
  218. client *unlink_client(chead,cltptr)
  219.     client *chead,*cltptr;
  220. {
  221.     if (chead == NULL)
  222.        return(NULL);
  223.     else
  224.        if (chead == cltptr)
  225.        {
  226.           cltptr->clnt_flags |= DELETE;
  227.           return(chead->clnt_nptr);
  228.        }
  229.  
  230.     chead->clnt_nptr = unlink_client(chead->clnt_nptr,cltptr);
  231.     return(chead);
  232. }
  233.  
  234.  
  235. void free_client(cltptr)
  236.     client *cltptr;
  237. {
  238.     cltptr->clnt_nptr = clfree;
  239.     clfree = cltptr;
  240.     cfree_cnt++;
  241. }
  242.  
  243.  
  244. client *get_client()
  245. {
  246.    client *cltptr;
  247.    struct timeval   tp;
  248.    struct timezone  tzp;
  249.  
  250.  
  251.    if (cfree_cnt == 0)
  252.    {
  253.       dolog("No more free clients\n");
  254.       exit(1);
  255.    }
  256.  
  257.    if (clfree == NULL)
  258.    {
  259.       dolog("Unexpectedly no more free clients\n");
  260.       exit(1);
  261.    }
  262.  
  263.    cltptr = clfree;
  264.    clfree = clfree->clnt_nptr;
  265.    cfree_cnt--;
  266.    bzero(cltptr,sizeof(client));
  267.    gettimeofday(&tp, &tzp);
  268.    cltptr->clnt_tp.tv_sec = tp.tv_sec;
  269.    cltptr->clnt_tp.tv_usec = tp.tv_usec;
  270.    return(cltptr);
  271. }
  272.  
  273. void free_slist(sptr)
  274.     slist *sptr;
  275. {
  276.     sptr->snd_nptr = sfree;
  277.     sfree = sptr;
  278.     sfree_cnt++;
  279. }
  280.  
  281.  
  282. slist *get_slist()
  283. {
  284.    slist *sptr;
  285.  
  286.    if (sfree_cnt == 0)
  287.    {
  288.       dolog("No more free slists\n");
  289.       exit(1);
  290.    }
  291.  
  292.    if (sfree == NULL)
  293.    {
  294.       dolog("Unexpectedly no more free slists\n");
  295.       exit(1);
  296.    }
  297.  
  298.    sptr = sfree;
  299.    sfree = sfree->snd_nptr;
  300.    sfree_cnt--;
  301.    bzero(sptr,sizeof(slist));
  302.    return(sptr);
  303. }
  304.  
  305.  
  306. void remove_some_clients(conf_id)
  307.    int conf_id;
  308. {
  309.    client *cptr,*cptr1;
  310.  
  311.    cptr = chead;
  312.  
  313.    while (cptr != NULL)
  314.    {
  315.       cptr1 = cptr->clnt_nptr;
  316.       if ((cptr->clnt_flags & CLIENT) && (cptr->clnt_conf_id != conf_id))
  317.       {
  318.          dolog("client %s is being deleted due to conf-id mis-match\n",cptr->clnt_config.name);
  319.          write_msg(&cptr->clnt_addr,kMessageType1,ci_buf);
  320.          tmp_deny(cptr->clnt_addr.addr,HRATE+5);
  321.          hold_down_client(cptr);
  322.       }
  323.      cptr = cptr1;
  324.    }
  325. }
  326.  
  327.